package com.ff.neuropp.service;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ff.neuropp.domain.Contacts;
import com.ff.neuropp.domain.Course;
import com.ff.neuropp.domain.IosUser;
import com.ff.neuropp.domain.Order;
import com.ff.neuropp.domain.Resume;
import com.ff.neuropp.repository.ContactsRepository;
import com.ff.neuropp.repository.IosUserRepository;
import com.ff.neuropp.repository.OrderRepository;
import com.ff.neuropp.repository.ResumeRepository;
import com.ff.neuropp.util.ResultTool;


/**
 * 通讯录服务
 */
@Service
public class AppConstactsService {

    @Resource
    private OrderRepository orderRepository;

    @Resource
    private ContactsRepository contactsRepository;


	@Autowired
    private IosUserRepository iosUserRepository;
	
	@Resource
    private ResumeRepository resumeRepository;

    /**
     * 通讯录
     * @param userId
     * @return
     */
    public Map<String,Object> constacts(String userId){
        List<Map<String,Object>> list = orderRepository.contacts(userId);
        return ResultTool.genSuccessResult(list);
    }

    /**
     * 好友关系建立
     * @return
     */
    public Map<String,Object> mapping(Contacts param){
        param.setApplyStatus(1);
        param.setUpdateTime(new Date());
        param.setCreateTime(new Date());
        Contacts result = contactsRepository.save(param);
        return ResultTool.genSuccessResult(result);
    }

    /**
     * 好友关系更新
     * @return
     */
    public Map<String,Object> update(Contacts param){
        int flag = contactsRepository.update(param.getStatus(),
                                    new Date(),
                                    param.getApplyStatus(),
                                    param.getReason(),
                                    param.getUserId(),
                                    param.getContactId());
        if(flag>0){
            return ResultTool.genSuccessResult("更新成功");
        }
        return ResultTool.genSuccessResult("更新失败");
    }

    /**
        * 查詢出用戶詳情
     * @param userId
     * @return
     */
	public Map<String, Object> detail(String userId) {
		 IosUser user =  iosUserRepository.findByUserId(userId);
		 //个人履历
		 List<Resume>  list= resumeRepository.findAllByUserId(userId);
		 user.setResumeList(list);
		 List<Course>  courseList = new ArrayList<>();
		 //查询通讯录好友正在上的课程
		 List<Order> orderList =orderRepository.findByOrderStatusAndUserId(1,userId);
		 for(Order order: orderList) {
			 courseList.add(order.getCourse());	
		 }
		 user.setCourseList(courseList);
		 return ResultTool.genSuccessResult(user);
	}

}
